﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Sinbiota.Web.Models
{
    public class SpeciesProfile
    {
        public string ScientificName { get; set; }
        public int Id { get; set; }
        public IList<VernacularName> VernacularNames { get; private set; }
        public IList<TaxonConcept> TaxonConcepts { get; private set; }
        public IList<DataObject> DataObjects { get; private set; }

        public class DataObject
        {
            public string DataType { get; set; }
            public string MimeType { get; set; }
            public string Description { get; set; }
            public string Title { get; set; }
            public string EOLMediaURL { get; set; }
            public string EOLThumbnailURL { get; set; }
            public float Rating { get; set; }
            
            public bool IsVideo()
            {
                return DataType.EndsWith("MovingImage");
            }
            public bool IsImage()
            {
                return DataType.EndsWith("StillImage");
            }
            public bool IsText()
            {
                return DataType.EndsWith("Text");
            }

        }

        public class TaxonConcept
        {
            public int Id { get; set; }
            public string SourceId { get; set; }
            public string ScientificName { get; set; }
            public string NameAccordingTo { get; set; }


        }

        public class VernacularName
        {
            public string Name { get; set; }
            public string Language { get; set; }
            public bool Preferred { get; set; }
        }

        public SpeciesProfile(int id)
        {
            ScientificName = "";
            Id = id;
            VernacularNames = new List<VernacularName>();
            DataObjects = new List<DataObject>();
            TaxonConcepts = new List<TaxonConcept>();
        }

        public SpeciesProfile(string json)
        {
            // Parse JSON:
            var jObject = JObject.Parse(json);
            ScientificName = (string)jObject["scientificName"];
            Id = (int)jObject["identifier"];
            // Taxon Concepts:
            TaxonConcepts = jObject["taxonConcepts"]
                .Select(c => new TaxonConcept
                    { Id = (int)c["identifier"], SourceId = (string)c["sourceIdentfier"], NameAccordingTo = (string)c["nameAccordingTo"], ScientificName = (string)c["scientificName"]
                    }).ToList();

            // Data Objects:
            DataObjects = jObject["dataObjects"].Select(obj => new DataObject
                                                                {
                                                                    DataType = (string) obj.SelectToken("dataType"), Description = (string) obj.SelectToken("description"), EOLMediaURL = (string) obj.SelectToken("eolMediaURL"), EOLThumbnailURL = (string) obj.SelectToken("eolThumbnailURL"), MimeType = (string) obj.SelectToken("mimeType"), Rating = (float) obj.SelectToken("dataRating"), Title = (string) obj.SelectToken("title"),
                                                                }).ToList();
            VernacularNames = jObject["vernacularNames"].Select(name => new VernacularName
            {
                Name = (string)name.SelectToken("vernacularName"),
                Language = (string)name.SelectToken("language"),
                Preferred = name.SelectToken("eol_preferred") == null ? false : (bool) name.SelectToken("eol_preferred")
            }).ToList();

        }

    }
}